package org.csu.mypetstorecmsapi.service;

import org.csu.mypetstorecmsapi.domain.Order;
import org.csu.mypetstorecmsapi.persistence.OrderDao;
import org.csu.mypetstorecmsapi.persistence.OrderStatusDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderService
{
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderStatusDao orderStatusDao;

    public List<Order> getOrders()
    {
        return orderDao.findAll();
    }

    public List<Order> getOrdersByStatus(String status)
    {
        return orderDao.findAll().stream().filter(order ->
                orderStatusDao.findById(order.getOrderId()).map(orderStatus ->
                        orderStatus.getStatus().equals(status)).orElse(false)).toList();
    }

    public void deleteOrder(int id)
    {
        orderDao.deleteById(id);
        orderStatusDao.deleteById(id);
    }

    public void setOrderStatus(int id, String status)
    {
        orderStatusDao.findById(id).ifPresent(orderStatus ->
        {
            orderStatus.setStatus(status);
            orderStatusDao.save(orderStatus);
        });
    }
}
